﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PdxDemo
{
    /// <summary>
    /// Interaction logic for NumericUpDown.xaml
    /// </summary>
    public partial class NumericUpDown : UserControl
    {
        public NumericUpDown()
        {
            InitializeComponent();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Increment();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Decrement();
        }

        void Increment()
        {
            Change(1);
        }

        void Decrement()
        {
            Change(-1);
        }

        void Change(int delta)
        {
            var val = (DataContext as PDX.IPropertyModel).UntypedValue;
            if (typeof(int).IsAssignableFrom(val.GetType()))
            {
                int value = (int)val;
                value += delta;
                (DataContext as PDX.IPropertyModel).UntypedValue = value;
            }
        }

        private void NumberTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                Increment();
                e.Handled = true;
            }
            else if (e.Key == Key.Down)
            {
                Decrement();
                e.Handled = true;
            }
        }
    }
}
